package kr.co.shoppingmall.noticeboard;

import java.awt.Component;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;




@Controller
@RequestMapping("/noticeboard")
public class NoticeBoardController {
	/* log4j */
	public static void showMessageDialog(Component parentComponent, Object message){}
	protected final Log logger = LogFactory.getLog(getClass());
	
	@Resource
	private NoticeBoardService noticeboardService;
	
	@RequestMapping("/NoticeWriteform.htm") //글쓰기
	public String noticewriteform(NoticeBoardDTO dto, HttpSession session){
		String id = (String)session.getAttribute("id");
		if(id == null){
			JOptionPane.showMessageDialog(null, "로그인 후 사용해 주세요.");
			return "redirect:/index.htm";
		}
		else{
			return "/noticeboard/NoticeWriteform";
		}
	}
	
	@RequestMapping("/NoticeWrite.htm")//글쓰기
	public String noticewrite(NoticeBoardDTO dto, HttpSession session){
		String id = (String)session.getAttribute("id");
		dto.setNboard_writer(id);
		noticeboardService.insertNoticeBoard(dto);
		return "redirect:/noticeboard/NoticeList.htm";
	}
	
	@RequestMapping("/NoticeList.htm") //리스트
	public String noticelist(@RequestParam(required = false, defaultValue = "1") int pg, Model model) {
		//pg 기본 값 : 1, 필수X
		int rowSize = 10;
		int start = (pg * rowSize) - (rowSize-1);
		int end = (pg * rowSize);
		HashMap map = new HashMap();
		map.put("start", start);
		map.put("end", end);
		List list = noticeboardService.getNoticeBoardList(map);
		int rowCount = noticeboardService.getCount();
		int linkSize = 10;
		int startLink = (pg-1) / linkSize * linkSize + 1;
		int endLink = (pg-1) / linkSize * linkSize + linkSize;
		int totalPage = rowCount / rowSize + (rowCount % rowSize == 0 ? 0 : 1);
		if(endLink > totalPage){
			endLink = totalPage;
		}
		int max = rowCount -((pg-1) * rowSize);
		model.addAttribute("pg", pg);
		model.addAttribute("linkSize", linkSize);
		model.addAttribute("startLink", startLink);
		model.addAttribute("endLink", endLink);
		model.addAttribute("totalPage", totalPage);
		model.addAttribute("max", max);
		model.addAttribute("list", list);
		return "/noticeboard/NoticeList";		
	}
	
	@RequestMapping("/NoticeDeleteform.htm") //글삭제
	public String deleteNoticeBoard(NoticeBoardDTO dto, HttpSession session)
	{
		String id = (String)session.getAttribute("id");
		if(id == null){
			JOptionPane.showMessageDialog(null, "로그인후 사용 해주세요");
			return "redirect:/index.htm";
		}
		else{
			return "/noticeboard/NoticeDeleteform";
		}
	}
	
	@RequestMapping("/NoticeDelete.htm")//글삭제
	public String noticedelete(NoticeBoardDTO dto, int pg)
	{
		noticeboardService.deleteNoticeBoard(dto);
		return "redirect:/noticeboard/NoticeList.htm?pg="+pg;
	}
	
	@RequestMapping("/NoticeRead.htm")//글읽기
	public String noticeread(int num, int pg, Model model)
	{
		noticeboardService.updateNoticeBoardHit(num);
		NoticeBoardDTO dto = noticeboardService.getNoticeBoard(num);
		model.addAttribute("b", dto);
		model.addAttribute("pg", pg);
		return "/noticeboard/NoticeRead";
	}
}//end
